<!-- 购物车列表 -->
<template>
	<view class="list">
		<view class="list-item" v-for="(item, index) in list" :key="index"
			@click="handleNavTo('/pagesC-shopping/goods-detail/index')">
			<view class="header">
				<text class="checkbox iconfont" :class="{'active': !!item.checked_all}"
					@click.stop="handleCheckedAllChange(index)">
					{{!!item.checked_all ? '&#xe62c;': '&#xe6a6;'}}
				</text>
				<text class="name">{{item.store_name}}</text>
				<text class="right-icon iconfont">&#xe61a;</text>
			</view>
			<view class="body">
				<view class="item" v-for="(sub, idx) in item.children" :key="idx">
					<text class="checkbox iconfont" :class="{'active': !!sub.checked}"
						@click.stop="handleCheckedChange(index, idx)">
						{{!!sub.checked ? '&#xe62c;': '&#xe6a6;'}}
					</text>
					<image class="pic" :src="'/pagesC-my/static/images/will-delete/3.png'" mode="aspectFit"></image>
					<view class="content">
						<text class="name">舒绝杀蚁饵剂红火蚁一窝端</text>
						<text class="category">规格：500ml</text>
						<view class="others">
							<view class="price">
								<text class="now">￥9.9</text>
								<text class="old">￥19.9</text>
							</view>
							<view class="animate">
								<view class="options" :class="{'active': !useShoppingCart.isShowDelete}">
									<button class="minus-btn iconfont" @click.stop="">&#xe60f;</button>
									<input class="num-input" type="number" value="1" @click.stop="" />
									<button class="plus-btn iconfont" @click.stop="">&#xe60e;</button>
								</view>
								<button class="delete-btn" @click.stop=""
									:class="{'active': useShoppingCart.isShowDelete}">删除</button>
							</view>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script setup>
	import { ref } from "vue";
	import { useShoppingCartStore } from '../store.js';

	const useShoppingCart = useShoppingCartStore();
	const list = ref([{
		store_name: '鱼米之乡农资站',
		checked_all: false,
		children: [{ checked: false }, { checked: false }]
	}, {
		store_name: '百丰农资站',
		checked_all: false,
		children: [{ checked: false }, { checked: false }]
	}, {
		store_name: '盛收农资站',
		checked_all: false,
		children: [{ checked: false }, { checked: false }]
	}]);

	const handleCheckedAllChange = (index) => {
		const checked = !list.value[index].checked_all;
		list.value[index].checked_all = checked;
		list.value[index].children.forEach(item => item.checked = checked);
	}

	const handleCheckedChange = (index, idx) => {
		list.value[index].children[idx].checked = !list.value[index].children[idx].checked;
		list.value[index].checked_all = list.value[index].children.every(item => item.checked);
	}

	const handleNavTo = (url) => {
		uni.navigateTo({
			url
		});
	}
</script>

<style lang="scss" scoped>
	.list {
		padding: 0 25rpx 20rpx;

		&-item {
			background-color: white;
			border-radius: 20rpx;
			padding: 20rpx;

			&+.list-item {
				margin-top: 20rpx;
			}

			.header {
				display: flex;
				align-items: center;

				.checkbox {
					flex-shrink: 0;
					font-size: 40rpx;
					color: #DCDCDC;
					margin-right: 10rpx;

					&.active {
						color: #FE3333;
					}
				}

				.name {
					flex-shrink: 0;
					font-weight: 500;
					font-size: 28rpx;
					color: #222222;
					line-height: 1.5;
					overflow: hidden;
					text-overflow: ellipsis;
					white-space: nowrap;
				}

				.right-icon {
					flex-shrink: 0;
					font-size: 24rpx;
					color: #222222;
				}
			}

			.item {
				display: flex;
				align-items: center;
				margin-top: 20rpx;

				.checkbox {
					flex-shrink: 0;
					font-size: 40rpx;
					color: #DCDCDC;
					margin-right: 10rpx;

					&.active {
						color: #FE3333;
					}
				}

				.pic {
					flex-shrink: 0;
					width: 128rpx;
					height: 128rpx;
					background-color: #F7F7F7;
					border-radius: 8rpx;
					margin-left: 6rpx;
				}

				.content {
					flex-grow: 1;
					width: 0;
					height: 128rpx;
					display: flex;
					flex-direction: column;
					justify-content: space-between;
					padding-left: 17rpx;
					overflow: hidden;

					.name {
						font-weight: 500;
						font-size: 28rpx;
						color: #222222;
						line-height: 1.5;
						overflow: hidden;
						text-overflow: ellipsis;
						white-space: nowrap;
					}

					.category {
						font-size: 24rpx;
						color: #999999;
						line-height: 1.5;
					}

					.others {
						display: flex;
						align-items: center;
						justify-content: space-between;

						.price {
							.now {
								font-weight: bold;
								font-size: 32rpx;
								color: #FE3333;
							}

							.old {
								font-size: 26rpx;
								color: #999999;
								text-decoration: line-through;
								margin-left: 10rpx;
							}
						}

						.animate {
							position: relative;
							height: 52rpx;
						}

						.options {
							position: absolute;
							bottom: 0;
							right: 0;
							display: flex;
							align-items: center;
							transform: translateX(100%);
							transition: transform 0.2s;

							&.active {
								transform: translateX(0);
							}

							.minus-btn {
								width: 74rpx;
								height: 56rpx;
								font-size: 18rpx;
								color: #222222;
								text-align: center;
								line-height: 56rpx;
								border: 1rpx solid #E5E5E5;
								border-right-width: 0;
								border-radius: 30rpx 0 0 30rpx;
							}

							.num-input {
								width: 60rpx;
								height: 56rpx;
								font-size: 26rpx;
								color: #222222;
								text-align: center;
								line-height: 56rpx;
								border: 1rpx solid #E5E5E5;
								box-sizing: border-box;
							}

							.plus-btn {
								width: 74rpx;
								height: 56rpx;
								font-size: 18rpx;
								color: #222222;
								text-align: center;
								line-height: 56rpx;
								border: 1rpx solid #E5E5E5;
								border-left-width: 0;
								border-radius: 0 30rpx 30rpx 0;
							}
						}

						.delete-btn {
							position: absolute;
							bottom: 0;
							right: 0;
							width: 95rpx;
							height: 52rpx;
							font-weight: bold;
							font-size: 26rpx;
							color: #FFFFFF;
							text-align: center;
							line-height: 52rpx;
							background: #FE3333;
							border-radius: 26rpx;
							transform: translateX(100%);
							transition: transform 0.2s;

							&.active {
								transform: translateX(0);
							}

							&:active {
								transform: translateX(0) scale(0.9);
							}
						}
					}
				}
			}

		}
	}
</style>